package com.controller.after;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.entity.Attachment;
import com.entity.News;
import com.entity.NewsInformation;
import com.service.AttachmentService;
import com.service.NewsInformationService;
import com.service.NewsService;

@Controller
@RequestMapping("/NewsInforContr/")
public class NewsInforController {
	//
	@Autowired
	private NewsInformationService NewsInforService;
	@Autowired
	private NewsService newsService;
	//附件
	@Autowired
	private AttachmentService fjService;
	
	
	@RequestMapping("/findNesw")
	@ResponseBody
	public AjaxRes findNesw(News o,Page<News> page)throws Exception{
		AjaxRes res=new AjaxRes();
		Page<News> indPages =newsService.findByPage(o, page);
		List<News> newsList=indPages.getResults();
		res.setSucceed(newsList, "数据获取成功");
		return res;
	}
	
	
	@RequestMapping("/ShoNews")
	@ResponseBody
	public AjaxRes ShoNews(NewsInformation n,Page<NewsInformation> page)throws Exception{
		AjaxRes res=new AjaxRes();
		Page<NewsInformation> indPages =NewsInforService.findByPageByNewid(new NewsInformation(), page,n.getNewsId());
		if(indPages.getResults()==null){
			res.setResMsg("数据查询为0");
		}else{
			res.setSucceed(indPages, "数据获取成功");
		}
		return res;
	}
	
	//分页
	@RequestMapping("/findByPage")
	@ResponseBody
	public AjaxRes findByPage(NewsInformation o,Page<NewsInformation> page)throws Exception{
		AjaxRes res=new AjaxRes();
		Page<NewsInformation> indPages =NewsInforService.findByPage(o, page);
		res.setSucceed(indPages, "数据获取成功");
		return res;
	}
	
	//转到添加页面
	@RequestMapping("/ShowAdd")
	public ModelAndView ShowAdd(){
		ModelAndView mv =  new ModelAndView();
		mv.addObject("state", "1");
		mv.setViewName("pages/NewsInfor/Add" );
		return mv;
	}
	
	
	//转到修改页面
	@RequestMapping("/ShowUpdate")
	public ModelAndView ShowUpdate(NewsInformation NewsInfor){
		String NewsIdArry[];
		if(NewsInfor.getNewsInformationId().contains(",")){
			NewsIdArry=NewsInfor.getNewsInformationId().split(",");
		}else{
			NewsIdArry=new String[]{NewsInfor.getNewsInformationId()};
		}
		System.out.println(NewsIdArry[0]);
		NewsInfor.setNewsInformationId(NewsIdArry[0]);
		ModelAndView mv =  new ModelAndView();
		List<NewsInformation> newsInforList=NewsInforService.find(NewsInfor);
		mv.addObject("newsInforList", newsInforList.get(0));
		mv.addObject("state", "2");
		mv.setViewName("pages/NewsInfor/Update");
		return mv;
	}
	
	@RequestMapping("/UpdateOrAdd")
	@ResponseBody
	public AjaxRes UpdateOrAdd(NewsInformation NewsInfor,String AttachmentId)throws Exception{
		AjaxRes res=new AjaxRes();
		System.out.println("NewsInfor"+NewsInfor);
		if(NewsInfor.getNewsInformationId()!=null && !NewsInfor.getNewsInformationId().equals("") ){
			try {
				NewsInforService.updateAll(NewsInfor,AttachmentId);
				System.out.println("修改成功");
				res.setResMsg("修改成功");
			} catch (Exception e) {
				res.setResMsg("修改失败");
				System.out.println("修改失败");
			}
		}else{
			try {
				NewsInforService.insterFj(NewsInfor, AttachmentId);
				res.setResMsg("添加成功");
			} catch (Exception e) {
				res.setResMsg("添加失败");
			}
		}
		return res;
	}
	
	
	@RequestMapping("/Delel")
	public ModelAndView Delel(NewsInformation NewsInfor,String idList)throws Exception{
		ModelAndView mv =  new ModelAndView();
		if(NewsInfor.getNewsInformationId()!=null){
			NewsInforService.delete(NewsInfor);
		}
		if(idList!=null){
			List<String>  gameids= java.util.Arrays.asList(idList.split(","));
			List<NewsInformation> NewsInforList=new ArrayList<NewsInformation>();
			for(int i=0;i<gameids.size();i++){
				NewsInformation ne=new NewsInformation();
				ne.setNewsInformationId(gameids.get(i));
				NewsInforList.add(ne);
			}
			NewsInforService.deleteBatch(NewsInforList);
		}
		mv.setViewName("pages/NewsInfor/NewsInforContent");
		return mv;
	}
	
	
	//查询用户附件转到修改页面
	@RequestMapping("/ShowAtt")
	@ResponseBody
	public AjaxRes ShowAtt(NewsInformation NewsInfo,Page<Attachment> page){
		AjaxRes res=new AjaxRes();
		Page<Attachment> indPages =fjService.findByPageId(new Attachment(), page,"newsInformation",NewsInfo.getNewsInformationId());
		res.setSucceed(indPages, "数据获取成功");
		return res;
	}
}
